﻿#pragma once

#include "audio/mixer.h"
#include "audio/ext.h"

using namespace anip;

class Sound;

class SoundGroup
{
public:
    SoundGroup(const wstring& n) :
        name(n) {}

    ~SoundGroup();

    wstring name;
    vector<Sound*> sounds;
};

extern vector<SoundGroup*> Groups;
extern SoundGroup* MainGroup;

enum class PlayState
{
    Stopped, Playing, Paused
};

class Sound
{
private:
    audio::Waveform* _wave = nullptr;
    audio::Playback* _play = nullptr;
    double _volume = 1;
    double _fadepoint = 0;
    enum class FadeType
    {
        Go, Stop, Pause
    } _fade;

    string _path;
    Sound() {}

public:
    Sound(const Sound&) = delete;
    ~Sound();

    static Sound* create(const string& path);

    wstring name;
    bool looping = false;
    double loopIn = 0;
    double loopOut = 0;

    function<void(Sound&)> 
        onPause, onUnpause, onStop, onPlay;

    double fadeTime = 0;

    PlayState status() const
    {
        if (_play == nullptr || _play->stopped()) return PlayState::Stopped;
        return _play->paused() ? PlayState::Paused : PlayState::Playing;
    }

    string path() const { return _path; }
    double runningTime() const { return _wave->runningTime(); }
    double volume() const { return _volume; }

    double currentPos() const
    {
        if (_play == nullptr || _play->stopped()) return 0;
        return _play->getTimePosition();
    }

    void setVolume(double v);
    void setPos(double v);

    void instantiate();
    void start();
    void stop();
    void pause();
    void unpause();

    void writeConfig();
    bool readConfig();
};